package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    /**
     * 角色分页查询
     * @param pageNum
     * @param pageSize
     * @param dto
     * @return
     */
    @Override
    public PageBean<RoleVo> pageQuery(Integer pageNum, Integer pageSize, RoleDto dto) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        Page<Role> page = roleMapper.selectPage(dto);
        return PageBean.of(page,RoleVo.class);
    }
    /**
     * 角色添加
     */
    @Override
    public void addRole(RoleDto dto) {
        Role role = BeanUtil.toBean(dto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 角色修改
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public void updateRole(RoleDto dto) {
        Role role = BeanUtil.toBean(dto, Role.class);
        roleMapper.updateByPrimaryKeySelective(role);

        String[] checkedResourceNos = dto.getCheckedResourceNos();

        if(ArrayUtil.isEmpty(checkedResourceNos)){
            return;
        }

        roleResourceMapper.deleteByRoleId(dto.getId());

        List<RoleResource> list = new ArrayList<>();
        for (String s : checkedResourceNos) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(dto.getId());
            roleResource.setResourceNo(s);
            roleResource.setDataState("0");
            list.add(roleResource);
        }
        roleResourceMapper.batchInsert(list);
    }

    /**
     * 根据角色查询选中的资源数据
     * @return
     */
    @Override
    public Set<String> getById(Long roleId) {
        Set<String> set = roleResourceMapper.selectResourceNoByRoleId(roleId);
        return set;
    }

    /**
     * 根据Id删除角色
     * @return
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        if(role.getDataState().equals("0")){
            throw new RuntimeException("启用状态不可删除!");
        }

        roleMapper.deleteByPrimaryKey(id);
        roleResourceMapper.deleteByRoleId(id);
    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<RoleVo> getAll() {
        List<Role> list = roleMapper.list();
        List<RoleVo> collect = list.stream()
                .map(role -> BeanUtil.toBean(role, RoleVo.class))
                .collect(Collectors.toList());
        return collect;
    }
}
